﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MisClient
{
    public partial class FrmUnsalableGoods : BaseForm
    {
        private IBLL.IDepart departBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteDepart();
        private IBLL.ISacf sacfBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSacf();
        private IBLL.IGoods goodsBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteGoods();
        private IBLL.IType typeTjBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteType();
        private IBLL.ICustomer cusBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteCustomer();
        private IBLL.ISupply supplyBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSupply();
        private IBLL.IFkType typeBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteFkType();

        private WinPrint winPrint = new WinPrint();

        private int dataGridView = 0;
        private int printType = 1;

        public FrmUnsalableGoods()
        {
            InitializeComponent();
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmUnsalableGoods_Load(object sender, EventArgs e)
        {
            this.openEndDate.Format = DateTimePickerFormat.Custom;
            this.openEndDate.CustomFormat = " ";

            this.openStarDate.Value = DateTime.Now.Date.AddDays(-1);
            this.txtDepCode.Text = accountUser.DeptCode;
            this.starDate.Focus();
        }

        private void openEndDate_ValueChanged(object sender, EventArgs e)
        {
            this.openEndDate.Format = DateTimePickerFormat.Long;
            this.openEndDate.CustomFormat = null;
        }

        private void txtDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
                {
                    MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
                if (depart == null)
                {
                    MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                this.txtBoxDep.Text = depart.DepName;
                this.txtBei.Focus();
            }
        }

        private void txtBei_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.textBox1.Focus();
            }
            
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtID.Focus();
            }
            
        }

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtID.Text.Trim()))
                {
                    MisEntity.Goods goods = goodsBLL.EndsWithID(this.txtID.Text.Trim());
                    if (goods == null)
                    {
                        MessageBox.Show("无此商品,请先设置！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtID.Focus();
                        return;
                    }
                    this.txtGoods.Text = goods.GOODS;
                    this.txtDM.Text = goods.DM;
                    this.txtID.Text = goods.ID;
                }
                this.textBox2.Focus();
            }
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.textBox3.Focus();
            }
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtGysCode.Focus();
            }
            
        }

        private void txtGysCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtGysCode.Text.Trim()))
                {
                    MisModel.Supply supply = supplyBLL.GetById(this.txtGysCode.Text.Trim());
                    if (supply == null)
                    {
                        MessageBox.Show("供应商不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtGysCode.Focus();
                        return;
                    }
                    else
                        this.txtGysName.Text = supply.SuppName;
                }
                this.txtFKID.Focus();
            }  
        }

        private void txtFKID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtFKID.Text.Trim()))
                {
                    MisModel.FKType fkType = typeBLL.GetById(this.txtFKID.Text.Trim());
                    if (fkType == null)
                    {
                        MessageBox.Show("无此代码或不属于此结算类型！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtFKID.Focus();
                        return;
                    }
                    this.textBox4.Text = fkType.FkType;
                }
                this.comboBox4.Focus();
            }
        }

        private void comboBox4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtPrice1.Focus();
            }
            
        }

        private void txtPrice1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtPrice2.Focus();
            }
            
        }

        private void txtPrice2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.comboBox1.Focus();
            }
            
        }

        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.textBox6.Focus();
            }
            
        }

        private void textBox6_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.textBox5.Focus();
            }
            
        }

        private void textBox5_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.comboBox3.Focus();
            }
            
        }

        private void comboBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.comboBox5.Focus();
            }
            
        }

        private void comboBox5_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.comboBox2.Focus();
            }
            
        }

        private void comboBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.comboBox6.Focus();
            }
            
        }

        private void comboBox6_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.textBox8.Focus();
            }
            
        }

        private void textBox8_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.textBox7.Focus();
            }
            
        }

        private void textBox7_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnOk.Focus();
            }
            
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
            {
                MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
            if (depart == null)
            {
                MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            StringBuilder sb = new StringBuilder();
            StringBuilder sb1 = new StringBuilder();
            this.dataGridView1.Rows.Clear();
            sb.Append(" 1=1 ");
            sb1.Append(" 1=1 ");
            if (!this.txtDepCode.Text.Trim().Equals("1"))
            {
                sb.Append("and m.dep_id=@depId");
                sb1.Append("and a.DEP_ID=@depId");
                dictionary.Add("@depId", this.txtDepCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtID.Text.Trim()))
            {
                sb.Append(" and m.id=@id");
                dictionary.Add("@id", this.txtID.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtFKID.Text.Trim()))
            {
                sb.Append(" and m.type_js=@typeJs");
                dictionary.Add("@typeJs", this.txtFKID.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtGysCode.Text.Trim()))
            {
                sb.Append(" and m.supp_id=@suppId");
                dictionary.Add("@suppId", this.txtGysCode.Text.Trim());
            }
            sb.Append(" and M.DATE_last<=@dateLast");
            DateTime time = DateTime.Parse(this.starDate.Value.ToShortDateString());
            dictionary.Add("@dateLast", time.ToShortDateString());
            string tjStr = this.comboBox3.SelectedItem.ToString();
            if ("可销售".Equals(tjStr))
                sb.Append(" AND M.F1!='N'");
            else if ("停止销售".Equals(tjStr))
                sb.Append(" AND M.F1='N'");
            tjStr = this.comboBox5.SelectedItem.ToString();
            if ("记入库存".Equals(tjStr))
                sb.Append(" AND M.F2<>!='N'");
            else if ("不记入库存".Equals(tjStr))
                sb.Append(" AND M.F2='N'");
            tjStr = this.comboBox2.SelectedItem.ToString();
            if ("需整数卖".Equals(tjStr))
                sb.Append(" AND M.F3<>!='N'");
            else if ("不记入库存".Equals(tjStr))
                sb.Append(" AND M.F3='N'");
            tjStr = this.comboBox4.SelectedItem.ToString();
            if ("间于".Equals(tjStr))
            {
                if (!string.IsNullOrEmpty(this.txtPrice1.Text.Trim()))
                {
                    sb.Append(" AND M.price1>=@minPrice");
                    dictionary.Add("@minPrice", this.txtPrice1.Text.Trim());
                }
                if (!string.IsNullOrEmpty(this.txtPrice2.Text.Trim()))
                {
                    sb.Append(" AND M.price1<=@maxPrice");
                    dictionary.Add("@maxPrice", this.txtPrice2.Text.Trim());
                }
            }
            else
                sb.Append(" and m.price1>=0");
            tjStr = this.comboBox1.SelectedItem.ToString();
            if ("间于".Equals(tjStr))
            {
                if (!string.IsNullOrEmpty(this.textBox6.Text.Trim()))
                {
                    sb.Append(" AND M.je1>=@minje1");
                    dictionary.Add("@minje1", this.textBox6.Text.Trim());
                }
                if (!string.IsNullOrEmpty(this.textBox5.Text.Trim()))
                {
                    sb.Append(" AND M.je1<=@maxje1");
                    dictionary.Add("@maxje1", this.textBox5.Text.Trim());
                }
            }
            else if ("非零".Equals(tjStr))
                sb.Append(" AND M.je1!=0");
            tjStr = this.comboBox6.SelectedItem.ToString();
            if ("间于".Equals(tjStr))
            {
                if (!string.IsNullOrEmpty(this.textBox6.Text.Trim()))
                {
                    sb.Append(" AND M.sl1>=@minsl1");
                    dictionary.Add("@minsl1", this.textBox6.Text.Trim());
                }
                if (!string.IsNullOrEmpty(this.textBox5.Text.Trim()))
                {
                    sb.Append(" AND M.sl1<=@maxsl1");
                    dictionary.Add("@maxsl1", this.textBox5.Text.Trim());
                }
            }
            else if ("非零".Equals(tjStr))
                sb.Append(" AND ( not (M.sl1=0 AND M.je1=0))");
            sb.Append(" AND M.SUPP_ID=D.SUPP_ID");

            sb1.Append(" and a.date>=@startDate");
            dictionary.Add("@startDate", this.openStarDate.Value.ToShortDateString());
            if (!string.IsNullOrEmpty(this.openEndDate.Text.ToString().Trim()))
            {
                sb1.Append(" and a.date<=@endDate");
                time = time.Date.AddDays(1);
                dictionary.Add("@endDate", time.ToShortDateString());
            }

            StringBuilder dataSb = new StringBuilder();
            time = DateTime.Parse(this.starDate.Value.ToShortDateString());
            string tableName = "";
            string tableStr = string.Format("{0}月底库存", time.ToString("yyMM"));
            DateTime time1 = DateTime.Now.Date;
            if (time1.ToShortDateString().Equals(time.ToShortDateString()))
            {
                tableStr = "当前库存";
                tableName = "GOODS_TAB";
            }
            else
            {
                time1 = time1.AddDays(-1);
                if (time1.ToShortDateString().Equals(time.ToShortDateString()))
                {
                    tableStr = "昨日库存";
                    tableName = "HOLD1GOODS";
                }
                else
                {
                    time1.AddDays(1);
                    if (time1.ToString("yyyyMM").Equals(time.ToString("yyyyMM")))
                    {
                        tableStr = "当前库存";
                        tableName = "GOODS_TAB";
                    }
                    else
                        tableName = string.Format("H{0}GOODS", time.ToString("yyMM"));
                }
            }
            if (!"GOODS_TAB".Equals(tableName))
            {
                DataTable tableDt = goodsBLL.getTableData(tableName);
                if (tableDt == null || tableDt.Rows.Count <= 0)
                {
                    MessageBox.Show(string.Format("查无{0}{1}备份记录！", tableStr, tableName), "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }


            DataTable dt = goodsBLL.GetGoodsEnquiriesAll(sb.ToString(), sb1.ToString(), tableName, dictionary);
            decimal slp = 0;
            decimal price1 = 0;
            decimal price2 = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    String[] DataRow = new String[24];
                    DataRow[0] = dt.Rows[i][0].ToString();
                    DataRow[1] = dt.Rows[i][1].ToString();
                    DataRow[2] = dt.Rows[i][2].ToString();
                    DataRow[3] = dt.Rows[i][3].ToString();
                    DataRow[4] = dt.Rows[i][4].ToString();
                    DataRow[5] = dt.Rows[i][5].ToString();
                    DataRow[6] = dt.Rows[i][6].ToString();
                    DataRow[7] = dt.Rows[i][7].ToString();
                    DataRow[8] = string.Format("{0}/{1}", dt.Rows[i][8].ToString(), dt.Rows[i][9].ToString());
                    DataRow[9] = dt.Rows[i][10].ToString();
                    DataRow[10] = dt.Rows[i][11].ToString();
                    DataRow[11] = string.Format("{0} {1}", dt.Rows[i][12].ToString(), dt.Rows[i][13].ToString()); 
                    DataRow[12] = dt.Rows[i][14].ToString();
                    DataRow[13] = dt.Rows[i][15].ToString();
                    DataRow[14] = dt.Rows[i][16].ToString();
                    DataRow[15] = dt.Rows[i][17].ToString();
                    DataRow[16] = dt.Rows[i][18].ToString();
                    DataRow[17] = dt.Rows[i][19].ToString();
                    DataRow[18] = dt.Rows[i][20].ToString();
                    DataRow[19] = dt.Rows[i][21].ToString();
                    DataRow[20] = dt.Rows[i][22].ToString();
                    DataRow[21] = dt.Rows[i][23].ToString();
                    DataRow[22] = dt.Rows[i][24].ToString();
                    DataRow[23] = dt.Rows[i][25].ToString();
                    slp += decimal.Parse(DataRow[2]);
                    price1 += decimal.Parse(DataRow[4]);
                    price2 += decimal.Parse(DataRow[5]);
                    this.dataGridView1.Rows.Add(DataRow);
                }
                this.label17.Text = string.Format("● 当前库存共{3}条 合计数量：{0} 进货成本：{1} 销价金额：{2}", (int)slp, price1, price2, dt.Rows.Count);
            }
            else
            {
                this.label17.Text = "";
                MessageBox.Show("查无符合条件的记录！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            Point mousePoint = MousePosition;
            Point point = this.PointToClient(mousePoint);
            this.contextMenuStrip1.Show(this, point);
        }

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void starDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.openStarDate.Focus();
            } 
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if (1 == this.printType)
                webPrint1(e);
            else if (2 == this.printType)
                webPrint2(e);
        }

        private void btnPri2_Click(object sender, EventArgs e)
        {
            this.dataGridView = 0;
            this.printType = 1;
            this.printPreviewDialog1.Document = this.printDocument1;
            this.printPreviewDialog1.PrintPreviewControl.Zoom = 1.0;
            this.printPreviewDialog1.ShowDialog();
        }

        private void btnPri3_Click(object sender, EventArgs e)
        {
            this.dataGridView = 0;
            this.printType = 2;
            this.printPreviewDialog1.Document = this.printDocument1;
            this.printPreviewDialog1.PrintPreviewControl.Zoom = 1.0;
            this.printPreviewDialog1.ShowDialog();
        }

        private void webPrint1(System.Drawing.Printing.PrintPageEventArgs e)
        {
            string _Title = "新江厦汽车销售公司进货单打印";
            Font _TitleFont = new Font("宋体", 20, FontStyle.Bold);
            Point _CurrentPoint = new Point(30, 30);
            _CurrentPoint.X = (e.PageBounds.Width) / 2 - (int)(e.Graphics.MeasureString(_Title, _TitleFont).Width) / 2;
            e.Graphics.DrawString(_Title, _TitleFont, new SolidBrush(Color.Black), _CurrentPoint);
            StringBuilder sb = new StringBuilder();
            decimal sp = 0;
            decimal price1 = 0;
            decimal price2 = 0;
            sb.Append(string.Format("日期：{0}至{1}\n", this.openStarDate.Value.ToShortDateString(), this.openEndDate.Value.ToShortDateString()));
            sb.Append(string.Format("部门：{0}\n", this.txtDepCode.Text.Trim()));
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append("商品名称               库存数 单位   销价    进货成本    批次  供应商    末次进货\n");
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            if (this.dataGridView1.Rows.Count > 0)
            {
                DataGridViewRow dr = null;
                for (int i = dataGridView; i < this.dataGridView1.RowCount; i++, dataGridView++)
                {
                    dr = this.dataGridView1.Rows[i];
                    sb.Append(winPrint.getGoodsStr(20, dr.Cells[1].Value.ToString()));
                    sb.Append(winPrint.getLenStr(9, decimal.Parse(dr.Cells[2].Value.ToString()).ToString("f2"))).Append(" ");
                    sp += decimal.Parse(dr.Cells[2].Value.ToString());
                    sb.Append(dr.Cells[3].Value.ToString()).Append(" ");
                    sb.Append(winPrint.getLenStr(10, decimal.Parse(dr.Cells[5].Value.ToString()).ToString("f2"))).Append(" ");
                    price1 += decimal.Parse(dr.Cells[5].Value.ToString());
                    sb.Append(winPrint.getLenStr(10, decimal.Parse(dr.Cells[4].Value.ToString()).ToString("f2")));
                    price2 += decimal.Parse(dr.Cells[4].Value.ToString());
                    sb.Append(winPrint.getLenStr(7, dr.Cells[8].Value.ToString())).Append(" ");
                    sb.Append(dr.Cells[11].Value.ToString().Split(' ')[0]).Append(" ");
                    sb.Append(dr.Cells[6].Value.ToString()).Append("\n");
                    if (i != 0 && i % 44 == 0)
                    {
                        dataGridView++;
                        break;
                    }
                }
            }
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(string.Format("合计数量：{0}  销价金额：{1}    进货成本：{2}   差价总额：{3}", sp.ToString("f2"), price1.ToString("f2"), price2.ToString("f2"), (price1-price2).ToString("f2"))).Append("\n");
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(string.Format("制表：{0} 姓名{1}", accountUser.UserCode,accountUser.Oper));

            _CurrentPoint.X = 30;
            _CurrentPoint.Y += (int)(e.Graphics.MeasureString(_Title, _TitleFont).Height) + 20;
            Font _ContentFont = new Font("宋体", 12);
            e.Graphics.DrawString(sb.ToString(), _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            if (dataGridView < this.dataGridView1.RowCount - 1)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
        }

        private void webPrint2(System.Drawing.Printing.PrintPageEventArgs e)
        {
            string _Title = "新江厦汽车销售公司进货单打印二";
            Font _TitleFont = new Font("宋体", 20, FontStyle.Bold);
            Point _CurrentPoint = new Point(30, 30);
            _CurrentPoint.X = (e.PageBounds.Width) / 2 - (int)(e.Graphics.MeasureString(_Title, _TitleFont).Width) / 2;
            e.Graphics.DrawString(_Title, _TitleFont, new SolidBrush(Color.Black), _CurrentPoint);
            StringBuilder sb = new StringBuilder();
            decimal sp = 0;
            decimal price1 = 0;
            decimal price2 = 0;
            sb.Append(string.Format("日期：{0}\n", DateTime.Now.ToShortDateString()));
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append("商品编码      商品名称               库存数 单位   销价      进货成本    末次进货\n");
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            if (this.dataGridView1.Rows.Count > 0)
            {
                DataGridViewRow dr = null;
                for (int i = dataGridView; i < this.dataGridView1.RowCount; i++, dataGridView++)
                {
                    dr = this.dataGridView1.Rows[i];
                    sb.Append(dr.Cells[0].Value.ToString()).Append(" ");
                    sb.Append(winPrint.getGoodsStr(20, dr.Cells[1].Value.ToString()));
                    sb.Append(winPrint.getLenStr(9, decimal.Parse(dr.Cells[2].Value.ToString()).ToString("f2"))).Append(" ");
                    sp += decimal.Parse(dr.Cells[2].Value.ToString());
                    sb.Append(dr.Cells[3].Value.ToString()).Append(" ");
                    sb.Append(winPrint.getLenStr(10, decimal.Parse(dr.Cells[5].Value.ToString()).ToString("f2"))).Append(" ");
                    price1 += decimal.Parse(dr.Cells[5].Value.ToString());
                    sb.Append(winPrint.getLenStr(10, decimal.Parse(dr.Cells[4].Value.ToString()).ToString("f2"))).Append(" ");
                    price2 += decimal.Parse(dr.Cells[4].Value.ToString());
                    sb.Append(dr.Cells[6].Value.ToString()).Append("\n");
                    if (i != 0 && i % 44 == 0)
                    {
                        dataGridView++;
                        break;
                    }
                }
            }
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(string.Format("合计数量：{0}  销价金额：{1}    进货成本：{2}   差价总额：{3}", sp.ToString("f2"), price1.ToString("f2"), price2.ToString("f2"), (price1 - price2).ToString("f2"))).Append("\n");
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(string.Format("制表：{0} 姓名{1}", accountUser.UserCode, accountUser.Oper));

            _CurrentPoint.X = 30;
            _CurrentPoint.Y += (int)(e.Graphics.MeasureString(_Title, _TitleFont).Height) + 20;
            Font _ContentFont = new Font("宋体", 12);
            e.Graphics.DrawString(sb.ToString(), _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            if (dataGridView < this.dataGridView1.RowCount - 1)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
        }
    }
}
